package spring.cloud.feign.client;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;

@SpringBootApplication
@EnableFeignClients
public class FeignClientApplication {
    
    private static String KEY="ERROR_RATIO";

	public static void main(String[] args) {
        SpringApplication.run(FeignClientApplication.class, args);
        initDegradeRule();
        initDegradeRule2();
     }

    private static void initDegradeRule() {
        List<DegradeRule> rules = new ArrayList<>();
        DegradeRule rule = new DegradeRule(KEY)
            .setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType())
            // Set ratio threshold to 50%.
            .setCount(0.1d)
            .setStatIntervalMs(3000)
            .setMinRequestAmount(3)
            // Retry timeout (in second)
            .setTimeWindow(10);
        rules.add(rule);
        DegradeRuleManager.loadRules(rules);
        System.out.println("Degrade rule loaded: " + rules);
      
        
    }
  
    private static void initDegradeRule2() {
        List<DegradeRule> rules = new ArrayList<>();
        DegradeRule rule = new DegradeRule("GET:http://spring-cloud-eureka-client/greeting")
            .setGrade(CircuitBreakerStrategy.ERROR_COUNT.getType())
            // Set ratio threshold to 50%.
            .setCount(1)
            .setStatIntervalMs(60000)
            .setMinRequestAmount(3)
            // Retry timeout (in second)
            .setTimeWindow(10);
        rules.add(rule);
        DegradeRuleManager.loadRules(rules);
        System.out.println("Degrade rule loaded: " + rules);
      
        
    }
}
